Server Structure
The server is defined in/internal/server/server.go:
Initialization
The server is created with connection details and a room manager instance:- Default address is
:2222 - Host key path defaults to
.ssh/id_ed25519(auto-generated if missing) - AI client is optional (only created if worker URL provided)
- Room manager is shared across all connections
Wish Server Configuration
The server is configured with Wish middleware:Middleware Stack
Duet uses two middleware layers:- Bubble Tea Middleware: Handles TUI creation for each SSH session
- Logging Middleware: Logs connection events and errors
Bubble Tea Handler
TheteaHandler function is called for each new SSH connection:
Session Information
- Username: Extracted from SSH session (defaults to “guest”)
- Renderer: Created per-session for style rendering
- PTY Info: Terminal type and color profile detection
- Alt Screen: Uses alternate screen buffer (terminal state is preserved on exit)
Color Profile Detection
The handler detects terminal capabilities:Lifecycle Management
Graceful Shutdown
The server listens for interrupt signals and shuts down gracefully:- Wait for SIGINT or SIGTERM signal
- Log shutdown message
- Create 10-second timeout context
- Call
srv.Shutdown()to close active connections - Return any shutdown errors
Connection Lifecycle
Authentication
Duet uses public key authentication via the host key:Host Key Generation
Wish automatically generates an Ed25519 host key if the file doesn’t exist:Per-Session Isolation
Each SSH session gets:- Unique Bubble Tea model instance (
ui.New(...)) - Unique client ID (
uuid.New().String()) - Separate event channel for room notifications
- Independent terminal subscription when joining a room
- Room Manager (singleton)
- Terminal instances (one per room, shared by all clients in that room)
- AI Client (if configured)
Configuration Options
Command-line flags inmain.go:
Example Usage
Error Handling
The server handles errors at multiple levels:Update method via ErrorMsg messages.
Security Considerations
- Host Key: Securely store the host key file with appropriate permissions (0600)
- No Password Auth: Only public key authentication is supported
- Workspace Isolation: Each room uses a separate workspace directory
- PTY Restrictions: Terminal processes run in isolated directories
- Rate limiting connections
- Authentication via SSH keys
- Network-level access controls
- Monitoring and logging